(function( $ ){
	var _paginator = {		
	};
	
	function selectPage( pageId, fireCallback ){		
		_paginator.currentPageId = pageId;
		pageIdCal();
		location.href="#" + _paginator.hashTag.replace(/__id__/, _paginator.currentPageId);

		_paginator.members.each(function(){	this.draw(); });
		if(fireCallback){
			_paginator.pageSelected(_paginator.currentPageId);
		}
	}
	function pageIdCal(){
		if(_paginator.currentPageId > pageCount()){
			_paginator.currentPageId = pageCount();
		}
		if(_paginator.currentPageId < 1){
			_paginator.currentPageId = 1;
		}
	}
	function pageCount(){
		return Math.ceil(_paginator.totalRecords / _paginator.itemsPerPage);		
	}
	function getStage(){
		var stage = new Object();
		var half = Math.ceil(_paginator.pagesPerStage / 2);
		stage.start = _paginator.currentPageId - half > 1? _paginator.currentPageId  - half  : 1;
		stage.end = Math.min(stage.start + _paginator.pagesPerStage - 1 , pageCount());
		return stage;
	}
	function getPrevPage(){
		return _paginator.currentPageId - 1 <= 1 ? 1 : _paginator.currentPageId - 1;
	}
	function getNextPage(){
		return new Number(_paginator.currentPageId) + 1 > pageCount() ? pageCount() : new Number(_paginator.currentPageId) + 1;
	}
	var methods = {		
		init : function( options ){
			_paginator = {
				itemsPerPage: 20,
				pagesPerStage: 10,
				totalRecords : 0,
				currentPageId : 1,
				hashTag: "!/__id__",
				head: true,
				tail: true,
				list: true,
				jump: true,
				prev: true,
				next: true,
				panel: false,
				headText: "<<",
				tailText: ">>",
				prevText: "<",
				nextText: ">",
				ellipse: "...",
				pageSelected: function(currentPageId){}
			};
			
			$.extend( _paginator, options );
			_paginator.members = this;
			return this.each(function(index, element){
				$(element).addClass("pagination");
				this.draw = function(){
					$(this).empty();

					if( _paginator.head ){
						// 生成首页按钮
						$(document.createElement("a"))
							.attr("pageId", 1)
							.attr("id", _paginator.hashTag.replace(/__id__/, 1))
							.attr("href", "#" + _paginator.hashTag.replace(/__id__/, 1))
							.text(_paginator.headText)
							.appendTo($(this));
					}
					if( _paginator.prev ){
						// 生成上一页链接
						$(document.createElement("a"))
							.attr("pageId", getPrevPage())
							.attr("id", _paginator.hashTag.replace(/__id__/, getPrevPage()))
							.attr("href", "#" + _paginator.hashTag.replace(/__id__/, getPrevPage()))
							.text(_paginator.prevText)
							.appendTo($(this));
					}
					if(_paginator.list){
						var stage = getStage();
						// 生成链接
						if(stage.start != 1){
							$(this).append("<ellipse>...</ellipse>");
						}
						for(var i = stage.start; i <= stage.end; i++){
							if(_paginator.currentPageId == i){
								$(document.createElement("tag"))
									.text(i)
									.appendTo($(this));
							}
							else{
								$(document.createElement("a"))
									.attr("id", _paginator.hashTag.replace(/__id__/, i))
									.attr("pageId", i)
									.attr("href", "#" + _paginator.hashTag.replace(/__id__/, i))
									.text(i)
									.appendTo($(this));
							}
						}
						if(stage.end != pageCount()){
							$(this).append("<ellipse>...</ellipse>");			
						}
					}
					
					if(_paginator.jump){
						//生成跳转
						var jumpInput = $(document.createElement("input"));
						
						jumpInput
							.keydown(function(e){if(e.which != 13 && ((e.which <= 48) || (e.which >=57))){e.preventDefault();}})
							.keypress(function(event){if(event.which == 13){selectPage($(this).val(), true);}})
								.width("2em");
						$(this).append("转到")
								.append(jumpInput);
					}
					
					if( _paginator.next ){
						// 生成下一页链接
						$(document.createElement("a"))
							.attr("pageId", getNextPage())
							.attr("id", _paginator.hashTag.replace(/__id__/, getNextPage()))
							.attr("href", "#" + _paginator.hashTag.replace(/__id__/, getNextPage()))
							.text(_paginator.nextText)
							.appendTo($(this));
					}
					if( _paginator.tail ){
						// 生成尾页链接
						var lastPageId = (pageCount() <= 1 ? 1 : pageCount());
						$(document.createElement("a"))
							.attr("pageId", lastPageId)
							.attr("id", _paginator.hashTag.replace(/__id__/, lastPageId))
							.attr("href", "#" + _paginator.hashTag.replace(/__id__/, lastPageId))
							.text(_paginator.tailText)
							.appendTo($(this));
					}
					$(this).children("a").bind("click", function(){ selectPage( $(this).attr("pageId"), true ); });
				};
				this.draw();
			});
		},
		selectPage : function( pageId, fireCallback ){
			selectPage( pageId, false );
			return this;
		},
		totalRecords : function( totalRecords ){
			if( totalRecords ){
				if( _paginator.totalRecords != totalRecords ){
					var oldPageId = _paginator.currentPageId;
					_paginator.totalRecords = totalRecords;
					pageIdCal();
					if( oldPageId != _paginator.currentPageId ){
						selectPage(_paginator.currentPageId, true);
					}
				}
				return this;
			}
			else{
				return _paginator.totalRecords;
			}			
		}
	}

	$.fn.paginator = function( method ){
		if(methods[method]){
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method == 'object' || !method ){
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Mehthod ' + method + ' does not exist on jQuery.paginator.');
		}		
	};
})( jQuery );